import os

# -*- coding: utf-8 -*-
import shutil

"""
 数据增强只有正样本 没有对于负样本的增强，建议解决这个问题
"""

# 源数据
IMG_DIR = "../voc2020/images/"
XML_DIR = "../voc2020/Annotations/"
# 待比较目标路径
AUG_XML_DIR = "../output/Annotations/"  # 存储增强后的XML文件夹路径
AUG_IMG_DIR = "../output/JPEGImages/"  # 存储增强后的影像文件夹路径
# 存储差异化的图片文件 到缓存temp==》dst
Src_XML_DIR = "../temp/dstxml/"
Src_IMG_DIR = "../temp/dstimg/"


def file_name():
    del_file(Src_XML_DIR)
    del_file(Src_IMG_DIR)
    del_file("../temp/srcxml/")
    del_file("../temp/srcimg/")
    src_list = []
    dst_list = []  # 这里的文件名称 比较规则 需要修改 # IM_0000_aug_0.xml
    files = os.listdir(AUG_IMG_DIR)
    for i, file in enumerate(files):
        filename = file.split('.')[0].split('_')[0]
        print(filename)
        src_list.append(filename)
    files2 = os.listdir(IMG_DIR)
    for j, file in enumerate(files2):
        filename = file.split('.')[0]
        print(filename)
        dst_list.append(filename)
    diff = set(dst_list).difference(set(src_list))  # 差集，在a中但不在b中的元素
    print(diff)
    print(len(diff))
    for x in diff:
        result = IMG_DIR + x + ".jpg";
        print(result)
        dst = Src_IMG_DIR + x + ".jpg";
        shutil.copy(result, dst)
    for x in diff:
        result = XML_DIR + x + ".xml";
        print(result)
        dst = Src_XML_DIR + x + ".xml";
        shutil.copy(result, dst)

def del_file(path):
    for i in os.listdir(path):
        path_file = os.path.join(path, i)  # 取文件绝对路径
        if os.path.isfile(path_file):
            os.remove(path_file)
        else:
            del_file(path_file)


if __name__ == '__main__':
    file_name()
